import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";

/**
 * 调整为多个类型，由于详情只会打开一个, 所以不需要调整结构
 * message    notice announce
 * 之前的消息  通知    公告
 * model - 站内消息
 * @date: 2018-8-9

 */
import { createPagination, getResponse } from 'utils/utils';
import { queryFileList as _queryFileList } from 'cpaas-front/lib/services/api';
import { changeRead as _changeRead, deleteMessage as _deleteMessage, queryMessage as _queryMessage, queryMessageDetail } from "../services/userMessageService";
export default {
  namespace: 'userMessage',
  state: {
    // type: {
    //   dataSource: [], // 消息数据
    //   pagination: false, // 分页参数
    // },
    currentType: 'message',
    // 当前切换的tab
    messageDetail: {},
    // 消息详情
    userMessageId: ''
  },
  effects: {
    // 查询数据
    queryMessage: /*#__PURE__*/_regeneratorRuntime.mark(function queryMessage(_ref, _ref2) {
      var payload, call, put, response, messageData, _payload, type;

      return _regeneratorRuntime.wrap(function queryMessage$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context.next = 4;
              return call(_queryMessage, payload);

            case 4:
              response = _context.sent;
              messageData = getResponse(response);

              if (!messageData) {
                _context.next = 10;
                break;
              }

              type = payload.type;
              _context.next = 10;
              return put({
                type: 'updateState',
                payload: (_payload = {}, _defineProperty(_payload, type, {
                  dataSource: messageData.content,
                  pagination: createPagination(messageData)
                }), _defineProperty(_payload, "currentType", type), _payload)
              });

            case 10:
            case "end":
              return _context.stop();
          }
        }
      }, queryMessage);
    }),
    // 获取文件
    queryFileList: /*#__PURE__*/_regeneratorRuntime.mark(function queryFileList(_ref3, _ref4) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function queryFileList$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call;
              _context2.next = 4;
              return call(_queryFileList, payload);

            case 4:
              res = _context2.sent;
              return _context2.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context2.stop();
          }
        }
      }, queryFileList);
    }),
    // 改变已读
    changeRead: /*#__PURE__*/_regeneratorRuntime.mark(function changeRead(_ref5, _ref6) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function changeRead$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              _context3.next = 4;
              return call(_changeRead, payload);

            case 4:
              response = _context3.sent;
              return _context3.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, changeRead);
    }),
    // 删除消息
    deleteMessage: /*#__PURE__*/_regeneratorRuntime.mark(function deleteMessage(_ref7, _ref8) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function deleteMessage$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call;
              _context4.next = 4;
              return call(_deleteMessage, payload);

            case 4:
              response = _context4.sent;
              return _context4.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, deleteMessage);
    }),
    // 查询消息详情
    queryDetail: /*#__PURE__*/_regeneratorRuntime.mark(function queryDetail(_ref9, _ref10) {
      var payload, call, put, response, messageDetail;
      return _regeneratorRuntime.wrap(function queryDetail$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call, put = _ref10.put;
              _context5.next = 4;
              return call(queryMessageDetail, payload);

            case 4:
              response = _context5.sent;
              messageDetail = getResponse(response);

              if (!messageDetail) {
                _context5.next = 9;
                break;
              }

              _context5.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  messageDetail: messageDetail
                }
              });

            case 9:
              return _context5.abrupt("return", messageDetail);

            case 10:
            case "end":
              return _context5.stop();
          }
        }
      }, queryDetail);
    })
  },
  reducers: {
    updateState: function updateState(state, action) {
      return _objectSpread(_objectSpread({}, state), action.payload);
    }
  }
};